package edu.nf.waimai.dao.shop.impl;


import edu.nf.util.DBUtils;
import edu.nf.waimai.dao.shop.ShopAndItemAndIemTypeDAO;
import edu.nf.waimai.model.ltem.Item;
import edu.nf.waimai.model.ltem.ItemType;
import edu.nf.waimai.model.shop.Shop;
import edu.nf.waimai.model.shop.dto.ShopAndItemAndIemType;
import org.nf.db.util.SqlExecutor;
import org.nf.db.util.result.BeanHandler;
import org.nf.db.util.result.BeanListHandler;

import java.util.List;

public class ShopAndItemAndItemTypeDAOImpl implements ShopAndItemAndIemTypeDAO {

    @Override
    public ShopAndItemAndIemType getShopAndItemAndIemType(Integer shopId) {
        //连接数据库对象
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        SqlExecutor sqlExecutor1 = new SqlExecutor(DBUtils.getConnection());
        SqlExecutor sqlExecutor2 = new SqlExecutor(DBUtils.getConnection());
        //sql执行器
        BeanHandler<Shop> shopBeanHandler = new BeanHandler(Shop.class);
        BeanListHandler<Item> itemBeanListHandler = new BeanListHandler<>(Item.class);
        BeanListHandler<ItemType> itemTypeBeanListHandler = new BeanListHandler<>(ItemType.class);
        //sql语句
        String shopSql = "SELECT shop_id, shop_img, shop_types_id, u_id, shop_name, shop_phone, shop_address, shop_datetime,shop_details FROM shop WHERE shop_id = ?";
        String itemSql = "SELECT item_id,shop_id, item_type_id, item_picture, item_name, item_price,item_state, item_details FROM items WHERE shop_id = ?;";
        String itemTypeSql = "SELECT item_type_id, shop_id, item_type_name FROM items_type WHERE shop_id = ?";
        //结果
        Shop shop = sqlExecutor.executeQuery(shopSql, shopBeanHandler, shopId);
        List<Item> items = sqlExecutor1.executeQuery(itemSql, itemBeanListHandler, shopId);
        List<ItemType> itemTypes = sqlExecutor2.executeQuery(itemTypeSql, itemTypeBeanListHandler, shopId);

        return new ShopAndItemAndIemType(shop,items,itemTypes);
    }
}
